package com.macro.mall.service.impl;

import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.macro.mall.mapper.SmsHomeNewProductMapper;
import com.macro.mall.model.SmsHomeNewProduct;
import com.macro.mall.model.SmsHomeNewProductExample;
import com.macro.mall.service.SmsHomeNewProductService;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/** 首页新品推荐管理Service实现类 Created by macro on 2018/11/6. */
@Service
public class SmsHomeNewProductServiceImpl implements SmsHomeNewProductService {
  @Autowired private SmsHomeNewProductMapper homeNewProductMapper;

  @Override
  public int create(List<SmsHomeNewProduct> homeNewProductList) {
    for (SmsHomeNewProduct SmsHomeNewProduct : homeNewProductList) {
      SmsHomeNewProduct.setRecommendStatus(1);
      SmsHomeNewProduct.setSort(0);
      homeNewProductMapper.insert(SmsHomeNewProduct);
    }
    return homeNewProductList.size();
  }

  @Override
  public int updateSort(Long id, Integer sort) {
    SmsHomeNewProduct homeNewProduct = new SmsHomeNewProduct();
    homeNewProduct.setId(id);
    homeNewProduct.setSort(sort);
    return homeNewProductMapper.updateByPrimaryKeySelective(homeNewProduct);
  }

  @Override
  public int delete(List<Long> ids) {
    SmsHomeNewProductExample example = new SmsHomeNewProductExample();
    example.createCriteria().andIdIn(ids);
    return homeNewProductMapper.deleteByExample(example);
  }

  @Override
  public int updateRecommendStatus(List<Long> ids, Integer recommendStatus) {
    SmsHomeNewProductExample example = new SmsHomeNewProductExample();
    example.createCriteria().andIdIn(ids);
    SmsHomeNewProduct record = new SmsHomeNewProduct();
    record.setRecommendStatus(recommendStatus);
    return homeNewProductMapper.updateByExampleSelective(record, example);
  }

  @Override
  public List<SmsHomeNewProduct> list(
      String productName, Integer recommendStatus, Integer pageSize, Integer pageNum) {
    PageHelper.startPage(pageNum, pageSize);
    SmsHomeNewProductExample example = new SmsHomeNewProductExample();
    SmsHomeNewProductExample.Criteria criteria = example.createCriteria();
    if (!StrUtil.isEmpty(productName)) {
      criteria.andProductNameLike("%" + productName + "%");
    }
    if (recommendStatus != null) {
      criteria.andRecommendStatusEqualTo(recommendStatus);
    }
    example.setOrderByClause("sort desc");
    return homeNewProductMapper.selectByExample(example);
  }
}
